package com.nbot.client;

import com.nbot.common.InterpreterFactory;
import com.nbot.common.command.BaseCommand;
import com.nbot.common.command.BotCommand;
import com.nbot.common.command.b2s.Command;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Date;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;

public class BotClient {

    public static final String IP = "localhost";//服务器地址
    public static final int PORT = 8000;//服务器端口号
    //待发送指令队列
    public static Queue<BotCommand> commandQueue = new ConcurrentLinkedQueue<BotCommand>();

    public static void main(String[] args) {
        //这个线程模拟机器人向消息队列发送消息（指令）
        new Thread(new Runnable() {
            @Override
            public void run() {
                //for (int i = 0; i < 10; i++) {
                    BotCommand command = new Command("二战苏330流量");

                    try {
                        Thread.sleep(new Random().nextInt(5000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    BotClient.commandQueue.add(command);
               // }
            }
        }).start();
        handler();
    }

    private static void handler() {
        try {
            Socket socket = new Socket(IP, PORT);
            //开启两个线程，一个负责读，一个负责写
            new Thread(new ClientReadHandlerThread(socket)).start();
            new Thread(new ClientWriteHandlerThread(socket)).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/*
 *处理读操作的线程
 */
class ClientReadHandlerThread implements Runnable {
    private Socket client;

    public ClientReadHandlerThread(Socket client) {
        System.out.println("new 了一个 ClientReadHandlerThread");
        this.client = client;
    }

    @Override
    public void run() {
        ObjectInputStream dis = null;
        try {
            while (true) {
                dis = new ObjectInputStream(client.getInputStream());
                BaseCommand receiveCommand = null;
                receiveCommand = (BaseCommand) dis.readObject();
                System.err.println(new Date() + "bot接收到server的一个[" + receiveCommand.getClass().getSimpleName() + "]指令: " + receiveCommand);
                InterpreterFactory.getInterpreter(receiveCommand).execute();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (dis != null) {
                    dis.close();
                }
                if (client != null) {
                    client = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

/*
 * 处理写操作的线程
 */
class ClientWriteHandlerThread implements Runnable {
    private Socket client;

    public ClientWriteHandlerThread(Socket client) {
        System.out.println("new 了一个 ClientWriteHandlerThread");
        this.client = client;
    }

    @Override
    public void run() {

        ObjectOutputStream dos = null;


        try {
            while (true) {
                BotCommand msg = BotClient.commandQueue.poll();
                if (msg != null) {

                    Thread.sleep(new Random().nextInt(1000));
                    dos = new ObjectOutputStream(client.getOutputStream());
                    System.out.println("bot向server发送一个指令" + msg);
                    System.out.println("当前Client队列长度为" + BotClient.commandQueue.size());
                    dos.writeObject(msg);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
                if (client != null) {
                    client = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
